﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClass;

namespace QuerySelectionLabelingTool
{
    public partial class ViewQuery : System.Web.UI.Page
    {
        int newsIndex;
        int taskID;
        protected void Page_Load(object sender, EventArgs e)
        {
            taskID = Convert.ToInt32(Request.QueryString["taskID"]);
            newsIndex = Convert.ToInt32(Request.QueryString["newsIndex"]);
            ViewQueryOperation op = new ViewQueryOperation();
            News news;
            bool isLast;
            int count;
            List<ViewQueryItem> listViewQuery = op.GetViewQuery(taskID,newsIndex,out news,out isLast,out count);
            this.labelIndex.Text = newsIndex + 1 + "/" + count;
            if (isLast == true)
            {
                this.Button1.Enabled = false;
                this.Button2.Enabled = false;
            }
            if (newsIndex == 0)
            {
                this.buttonPrevious.Enabled = false;
                this.buttonPrevious2.Enabled = false;
            }
            DisplayQueryViewInPage(listViewQuery, news);
        }

        void DisplayQueryViewInPage(List<ViewQueryItem> listViewQueryItem, News news)
        {
            this.labelTitle.Text = news.Title;
            this.labelContent.Text = news.Content;

            int rowIndex = 0;
            CoverageOperation coverageOpe = new CoverageOperation();
            while (true)
            {
                TableRow row = new TableRow();
                int completeExpCount = 0;
                foreach (ViewQueryItem item in listViewQueryItem)
                {
                    TableCell cell = new TableCell(); ;
                    if (rowIndex == 0)
                    {
                        cell.Text = item.Ex.Name;
                    }
                    else
                    {
                        if ((rowIndex - 1) < item.ListQueryControlData.Count)
                        {
                            QueryControlData queryControlData = item.ListQueryControlData.ElementAt(rowIndex - 1);
                            WebQueryControl queryControl = (WebQueryControl)Page.LoadControl("WebQueryControl.ascx");
                            queryControl.QueryText = queryControlData.Query;
                            queryControl.QueryLink = coverageOpe.EncodeUrl(queryControlData.Query, queryControlData.NewsID);
                            queryControl.StateText = CoverageOperation.GetPosition(queryControlData.Position);
                            cell.Controls.Add(queryControl);
                        }
                        else
                        {
                            ++completeExpCount;
                        }
                    }
                    row.Cells.Add(cell);
                }
                if (completeExpCount == listViewQueryItem.Count)
                {
                    break;
                }
                tableQueryView.Rows.Add(row);
                ++rowIndex;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewQuery.aspx?taskID="+taskID+"&newsIndex="+(newsIndex+1));
        }

        protected void buttonPrevious_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewQuery.aspx?taskID="+taskID+"&newsIndex="+(newsIndex-1));
        }
    }
}